import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const Button 	= $Lazy.load(()=>import('@antd/button'))
const CodeList 	= $Lazy.hook(()=>import('./cpt/code-list'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const PrintData = [{ name:'三联', value:1 }, { name:'六联', value:2 }, { name:'九联', value:3 }]
const width = '24%'
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		formData : [
			{ label: '当前条码号',	name: 'spec_code', 		width, readOnly:true,  mb:0	},
			{ label: '生成数量',		name: 'number',			width, type:'number', mb:0, value:1, onValue:({ value, ref }) =>{
				if(value < 1) setTimeout(()=> ref.setValue({ create_num: 1}),500 )
			}},
			{ label: '医院名称',		name: 'hospital_id',	width, type:'select', mb:0, loading:true, onValue:({ rows }) => {
				this.submit.hosp_name = rows ? rows.name : ''
			}},
			{ label: '',	 		name: 'link',			width,	 value:1, ml:10, mb:0, type:'select', data:PrintData, onValue: ({ value })=>{
				this.setState({ link: value})
			}},
		],
		link:PrintData[0].value
	}
	
	submit = {}

	componentDidMount(){ this.getSelect() }
	
	fetch = () => $http.submit(this,'barcode/info', { loading:false }).then(({ spec_code })=> this.formRef.setValue({ spec_code }) )
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 医院
			$fn.setRow( formData, 'hospital_id', await f.getCache({ api:'hospital' }) )
			// 
			this.setState({ formData })
		})
	}
			
	render(){
		const { submitLoading, data, link } = this.state
		return (
			<Page nobc title='条码生成'>
				{/* 搜索 */}
				<div className='ptb10 fx mb10 bcf'>
					{
						SubmitForm.call(this,{ 
							labelWidth	: 's6', 
							api			: 'barcode/add', 
							query		: ({ number, hosp_name, hospital_id }) =>  ( { type:1, number, hosp_name, hospital_id } ),
							onForm		: this.fetch,
							onRefresh	: ({ data, param}) => {
								this.fetch()
								this.setState({ data: data['lists'] })
							}
						})
					}
					<Button label='生成条码' 		loading={submitLoading} size='middle' className='mr10' code='F2' onClick={()=> {
						this.formRef.submit()
					}} />
					<Button label='大批量生成条码' 	loading={submitLoading} size='middle' className='mr10' ghost onClick={()=>$fn.pushTo(this,'morecode-generate')} />
					<Button label='自定义生成条码' 	loading={submitLoading} size='middle' ghost onClick={()=>$fn.pushTo(this,'customcode-generate')} />
				</div>
				<CodeList data={data} loading={submitLoading} link={link} />
			</Page>
		)
	}
}

export default Index